package ptumall.dao.shop;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import ptumall.model.shop.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface GoodsDao extends BaseMapper<Goods> {

    @Select("SELECT id, name, price, sales, thumb_url FROM goods " +
            "WHERE category_id = #{categoryId} AND status = 1 " +
            "ORDER BY sales DESC " +
            "LIMIT #{offset}, #{pageSize}")
    List<GoodsItem> selectByCategoryPage(@Param("categoryId") Long categoryId,
                                         @Param("offset") Integer offset,
                                         @Param("pageSize") Integer pageSize);

    @Select("SELECT COUNT(*) FROM goods WHERE category_id = #{categoryId} AND status = 1")
    Long countByCategory(@Param("categoryId") Long categoryId);

    // 商品基础信息+分类
    @Select("SELECT g.*, c.name as category_name FROM goods g " +
            "LEFT JOIN goods_category c ON g.category_id = c.id " +
            "WHERE g.id = #{goodsId} AND g.status = 1")
    GoodsDetail selectGoodsBase(@Param("goodsId") Long goodsId);

    // 查询商品规格
    @Select("SELECT * FROM goods_spec WHERE goods_id = #{goodsId} ORDER BY id")
    List<GoodsSpec> selectSpecsByGoodsId(@Param("goodsId") Long goodsId);

    // 查询商品SKU
    @Select("SELECT * FROM goods_sku WHERE goods_id = #{goodsId} AND status = 1")
    List<GoodsSku> selectSkusByGoodsId(@Param("goodsId") Long goodsId);

    // 查询商品相册
    @Select("SELECT url FROM goods_gallery WHERE goods_id = #{goodsId} ORDER BY sort")
    List<String> selectGalleryByGoodsId(@Param("goodsId") Long goodsId);

    //查询商品库存
    @Select("SELECT * FROM goods_sku WHERE goods_id = #{goodsId} ORDER BY sort")
    GoodsSku selectSkuById(@Param("skuId") Long skuId);

    //搜索商品
    @Select({
            "<script>",
            "SELECT id, name, price, sales, thumb_url as thumbUrl FROM goods",
            "WHERE status = 1",
            "<if test='keyword != null and keyword != \"\"'>",
            "AND (name LIKE CONCAT('%', #{keyword}, '%') OR description LIKE CONCAT('%', #{keyword}, '%'))",
            "</if>",
            "ORDER BY sales DESC, create_time DESC",
            "LIMIT #{offset}, #{pageSize}",
            "</script>"
    })
    List<GoodsItem> searchGoods(@Param("keyword") String keyword,
                                @Param("offset") Integer offset,
                                @Param("pageSize") Integer pageSize);

    @Select({
            "<script>",
            "SELECT COUNT(*) FROM goods",
            "WHERE status = 1",
            "<if test='keyword != null and keyword != \"\"'>",
            "AND (name LIKE CONCAT('%', #{keyword}, '%') OR description LIKE CONCAT('%', #{keyword}, '%'))",
            "</if>",
            "</script>"
    })
    Long countSearchGoods(@Param("keyword") String keyword);

    @Select("<script>" +
            "SELECT id, category_id, name, price, original_price, sales, thumb_url, status " +
            "FROM goods " +
            "WHERE status = #{status} " +
            "<if test='keyword != null and keyword != \"\"'>" +
            "   AND name LIKE CONCAT('%', #{keyword}, '%')" +
            "</if>" +
            "ORDER BY sales DESC" +
            "</script>")
    List<Goods> getsearchGoods(
            @Param("keyword") String keyword,
            @Param("status") Integer status
    );
}